package cn.hackcloud.concurrency.per;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * TaskHandler用于处理每一个提交的Request请求，由于TaskHandler将被
 * Thread执行，因此需要实现Runnable接口
 */
public class TaskHandler implements Runnable {
    private final Request request;

    public TaskHandler(Request request) {
        this.request = request;
    }

    @Override
    public void run() {
        System.out.println("begin handle " + request);
        slowly();
        System.out.println("begin handle " + request);
    }

    //模拟请求处理比较耗时，使得线程进入短暂的休眠阶段
    private void slowly() {
        try {
            TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(10));
        } catch (Exception e) {

        }
    }
}
